﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Script.Serialization;

namespace Omu.Awesome.Mvc.Helpers
{
    public static class PopupHelpers
    {
        public static MvcHtmlString PopupActionLink<T>(this HtmlHelper html, Expression<Action<T>> e, string text = null, object htmlAttributes = null, string prefix = null)
            where T : Controller
        {
            return
                MvcHtmlString.Create(
                    string.Format("<a href='javascript:{0}' {2} >{1}</a>",
                                  PopupAction(new UrlHelper(html.ViewContext.RequestContext), e, prefix),
                                  text ?? e.Name(), htmlAttributes.GetDictionary().ToHtml()));
        }

        public static MvcHtmlString PopupActionLink(this HtmlHelper html, string action, string text = null, IEnumerable<object> parameters = null, object htmlAttributes = null, string controller = null, string prefix = null)
        {
            return
                MvcHtmlString.Create(
                    string.Format("<a href='javascript:{0}' {2} >{1}</a>", PopupAction(new UrlHelper(html.ViewContext.RequestContext), action.ToLower(), parameters, controller, prefix),
                                  text ?? action, htmlAttributes.GetDictionary().ToHtml()));
        }

        public static MvcHtmlString PopupAction<T>(this UrlHelper url, Expression<Action<T>> e, string prefix = null) where T : Controller
        {
            return PopupAction(url, e.Name(), e.GetValues(), typeof(T).Controller(), prefix);
        }

        public static MvcHtmlString PopupAction(this UrlHelper url, string action, IEnumerable<object> parameters = null, string controller = null, string prefix = null)
        {
            var funcParams = parameters != null ?
                parameters.Aggregate("", (current, o) => current + new JavaScriptSerializer().Serialize(o) + ",").RemoveSuffix(",")
                : string.Empty;

            return MvcHtmlString.Create("callp" + prefix + (action + (controller ?? url.Controller())).ToLower() + "(" + funcParams + ")");
        }

        public static MvcHtmlString GetPopupId(this UrlHelper url, string action, string controller = null, string prefix = null)
        {
            return MvcHtmlString.Create("p" + prefix + (action + (controller ?? url.Controller())).ToLower());
        }

        public static MvcHtmlString MakePopup<T>(
            this HtmlHelper html,
            Expression<Action<T>> e,
            string title = null,
            IDictionary<string, string> buttons = null,
            int? height = null,
            int? width = null,
            bool? modal = null,
            bool? resizable = null,
            string prefix = null,
            string position = null,
            bool? fullScreen = null
            ) where T : Controller
        {
            return MakePopup(html, e.Name(), e.Params(), typeof(T).Controller(), title, buttons, height, width, modal, resizable, prefix, position, fullScreen);
        }

        public static MvcHtmlString MakePopup(this HtmlHelper html, string action, string[] parameters = null, string controller = null, string title = null, IDictionary<string, string> buttons = null, int? height = null, int? width = null, bool? modal = null, bool? resizable = null, string prefix = null, string position = null, bool? fullScreen = null)
        {
            return html.Partial(Settings.AwesomeFolder + "Popup.ascx",
                                new PopupInfo
                                    {
                                        Action = action,
                                        Controller = controller ?? html.Controller(),
                                        Title = title,
                                        Height = height ?? Settings.Popup.Height,
                                        Width = width ?? Settings.Popup.Width,
                                        Parameters = parameters ?? new string[] { },
                                        Buttons = buttons ?? new Dictionary<string, string>(),
                                        Modal = modal ?? Settings.Popup.Modal,
                                        Resizable = resizable ?? Settings.Popup.Resizable,
                                        Prefix = prefix,
                                        Position = position.JQPos(),
                                        FullScreen = fullScreen ?? false
                                    });
        }

        public static MvcHtmlString MakePopup(this HtmlHelper html, string name, string content, string title = null, IDictionary<string, string> buttons = null, int? height = null, int? width = null, bool? modal = null, bool? resizable = null, string prefix = null, string position = null, bool? fullScreen = null)
        {
            return html.Partial(Settings.AwesomeFolder + "Popup.ascx",
                                new PopupInfo
                                    {
                                        Action = name,
                                        Controller = html.Controller(),
                                        Content = content,
                                        Title = title,
                                        Height = height ?? Settings.Popup.Height,
                                        Width = width ?? Settings.Popup.Width,
                                        Buttons = buttons ?? new Dictionary<string, string>(),
                                        Modal = modal ?? Settings.Popup.Modal,
                                        Resizable = resizable ?? Settings.Popup.Resizable,
                                        Prefix = prefix,
                                        Position = position.JQPos(),
                                        Parameters = new string[] { },
                                        FullScreen = fullScreen ?? false
                                    });
        }

    }
}